#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("SerialAssistant-pan");
    system_init();
}

MainWindow::~MainWindow()
{
    delete ui;
}

/****************************************
 *     functions
 * ****************************************/
void MainWindow::system_init()
{
    // 寻找可用串口
    QStringList serialStrList;
    serialStrList = scanSerial();
    for (int i=0; i<serialStrList.size(); i++)
    {
        ui->comboBox_port->addItem(serialStrList[i]); // 在comboBox那添加串口号
    }
    //global_port.setParity(QSerialPort::NoParity);
    //global_port.setDataBits(QSerialPort::Data8);
    //global_port.setStopBits(QSerialPort::OneStop);
    ui->comboBox_DataBits->setCurrentIndex(3);
    connect(ui->btn_open,&QPushButton::clicked,this,&MainWindow::btn_open_port);
    connect(ui->btn_send,&QPushButton::clicked,this,&MainWindow::btn_send_data);
    connect(ui->btn_close,&QPushButton::clicked,this,&MainWindow::btn_close_port);
    connect(ui->btn_clear,&QPushButton::clicked,this,&MainWindow::btn_clear_window);
    connect(ui->btn_scan,&QPushButton::clicked,this,&MainWindow::btn_scan_port);
    connect(&global_port, &QSerialPort::readyRead, this,&MainWindow::readData);
    qDebug()<<"ok";
}
/****************************************
 *     slots
 * ****************************************/
void MainWindow::btn_open_port(bool)
{
    global_port.setPortName(ui->comboBox_port->currentText());
    //set Serial BaudRate
    switch (ui->comboBox_rate->currentIndex()) {
    case 0:
        global_port.setBaudRate(QSerialPort::Baud115200);
        break;
    case 1:
        global_port.setBaudRate(QSerialPort::Baud57600);
        break;
    case 2:
        global_port.setBaudRate(QSerialPort::Baud38400);
        break;
    case 3:
        global_port.setBaudRate(QSerialPort::Baud19200);
        break;
    case 4:
        global_port.setBaudRate(QSerialPort::Baud9600);
        break;
    case 5:
        global_port.setBaudRate(QSerialPort::Baud4800);
        break;
    case 6:
        global_port.setBaudRate(QSerialPort::Baud2400);
        break;
    case 7:
        global_port.setBaudRate(QSerialPort::Baud1200);
        break;
    default:
        break;
    }
    switch (ui->comboBox_Parity->currentIndex()) {
    case 0:
        global_port.setParity(QSerialPort::NoParity);
        break;
    case 1:
        global_port.setParity(QSerialPort::OddParity);
        break;
    case 2:
        global_port.setParity(QSerialPort::EvenParity);
        break;
    case 3:
        global_port.setParity(QSerialPort::MarkParity);
        break;
    case 4:
        global_port.setParity(QSerialPort::SpaceParity);
        break;
    default:
        break;
    }
    switch (ui->comboBox_DataBits->currentIndex()) {
    case 0:
        global_port.setDataBits(QSerialPort::Data5);
        break;
    case 1:
        global_port.setDataBits(QSerialPort::Data6);
        break;
    case 2:
        global_port.setDataBits(QSerialPort::Data7);
        break;
    case 3:
        global_port.setDataBits(QSerialPort::Data8);
        break;
    default:
        break;
    }
    switch (ui->comboBox_StopBits->currentIndex()) {
    case 0:
        global_port.setStopBits(QSerialPort::OneStop);
        break;
    case 1:
        global_port.setStopBits(QSerialPort::OneAndHalfStop);
        break;
    case 2:
        global_port.setStopBits(QSerialPort::TwoStop);
        break;
    default:
        break;
    }
    //open Serial port
    global_port.open(QSerialPort::ReadWrite);
    ui->comboBox_port->setEnabled(false);
    ui->comboBox_rate->setEnabled(false);
    ui->comboBox_Parity->setEnabled(false);
    ui->comboBox_DataBits->setEnabled(false);
    ui->comboBox_StopBits->setEnabled(false);
}

void MainWindow::btn_send_data(bool)
{
    qDebug()<<ui->line_send->text();
    QString data = ui->line_send->text();
    QByteArray Array = data.toLocal8Bit();
    global_port.write(Array);
}

void MainWindow::readData()
{
    QByteArray data = global_port.readAll();
    qDebug()<<data;
    QString string = QString::fromLocal8Bit(data);
    ui->save_text->insertPlainText(string);
}

void MainWindow::btn_close_port(bool)
{
    global_port.clear();
    global_port.close();
    ui->comboBox_port->setEnabled(true);
    ui->comboBox_rate->setEnabled(true);
    ui->comboBox_Parity->setEnabled(true);
    ui->comboBox_DataBits->setEnabled(true);
    ui->comboBox_StopBits->setEnabled(true);
}

void MainWindow::btn_clear_window(bool)
{
    ui->save_text->clear();
}

QStringList MainWindow::scanSerial()
{
    QStringList serialStrList;

    // 读取串口信息
    foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        global_port.setPort(info);
        // 判断端口能否以读写方式打开
        if(global_port.open(QIODevice::ReadWrite))
        {
            serialStrList.append(global_port.portName());
            global_port.close(); // 然后自动关闭等待人为开启（通过"打开串口按钮"）
        }
    }

    return serialStrList;

}

void MainWindow::btn_scan_port(bool)
{
    // 寻找可用串口
    QStringList serialStrList;
    serialStrList = scanSerial();
    for (int i=0; i<serialStrList.size(); i++)
    {
        ui->comboBox_port->addItem(serialStrList[i]); // 在comboBox那添加串口号
    }
}



void MainWindow::on_save_text_textChanged()
{
    ui->save_text->moveCursor(QTextCursor::End);
}
